<template>
  <el-dialog title="图纸管理"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal= false
             width="80%"
             center
             @close="cancel"
  >
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="图纸名称" prop="lineName">
        <el-input
          v-model="queryParams.lineName"
          placeholder="请输入图纸名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图号" prop="lineNumber">
        <el-input
          v-model="queryParams.lineNumber"
          placeholder="请输入图号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图纸描述" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入图纸描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <FileUpload
          :limit="100"
          :fileSize="500"
          :fileType="fileType"
          :value="pictureList"
          :isShowTip="isShowTip"
          :uploadData="uploadData"
          @onUploaded="handleUploaded"
        >
        </FileUpload>
      </el-col>
      <el-col :span="5" style="margin-top: 5px;" >
        <span style="color: red;">请上传格式为PDF的文件</span>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
        v-loading="loading"
        :data="drawinglineList"
        max-height="400"
        @cell-mouse-enter="handleCellEnter()"
        @cell-mouse-leave="handleCellLeave()"
    >
      <el-table-column label="图纸名称" align="center" prop="lineName" />
      <el-table-column label="图号" align="center" prop="lineNumber" />
      <el-table-column label="图纸描述" align="center" prop="remark">
        <div class="item" slot-scope="scope">
          <el-input
              class="item_input"
              v-model="scope.row.remark"
              size="mini"
              type="textarea"
              style="width: 100%;"
              :title="scope.row.remark"
              @blur="handleBlur(scope.row)"
          />

        </div>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleView(scope.row)"
          >查看图纸</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="open" width="1300px" append-to-body>
      <iframe
          :src="form.lineUrl"
          frameborder="0"
          scrolling="auto"
          style="width: 100%; height: 600px;"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
  </el-dialog>
</template>

<script>
import {drawingList, delDrawing, getDrawing, editDrawing} from "@/api/mes/pro/outsourcingorder";
import FileUpload from "./fileUpload.vue";

export default {
  name: "Drawing",

  components: { FileUpload },
  props: {
    orderLineId: null,
    optType: null,
  },
  data() {
    return {
      showFlag: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 图纸集Line表格数据
      drawinglineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 1000,
        orderLineId: this.orderLineId,
        lineName: null,
        lineNumber: null,
        lineUrl: null,
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null
      },
      // 表单参数
      form: {},
      // 上传类型
      fileType: ["pdf"],
      // 图纸列表
      pictureList: [],
      // 上传提示开关
      isShowTip: false,
      // 上传附带参数
      uploadData: { orderLineId: this.orderLineId },
      // 可修改单元格属性
      editProp: ['remark'],
    };
  },
  watch: {
    orderLineId: function (newVal, oldVal) {
      if (newVal !== oldVal && newVal !== null) {
        this.queryParams.orderLineId = newVal;
        this.uploadData.orderLineId = newVal;
      }
      this.getList();
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询图纸集Line列表 */
    getList() {
      this.loading = true;
      drawingList(this.queryParams).then(response => {
        this.drawinglineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit('close');
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderLineId: this.orderLineId,
        lineName: null,
        lineNumber: null,
        lineUrl: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null
      };
      this.resetForm("queryForm")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + row.lineNumber + '"的图纸？').then(function() {
        return delDrawing(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 上传成功
    handleUploaded(data) {
      this.$modal.msgSuccess("上传成功")
      this.getList();
    },
    // 查看图纸
    handleView(row) {
      this.reset();
      this.open = true;
      getDrawing(row.id).then(response => {
        this.form = response.data;
      })
    },
    // 列表单元格鼠标进入事件
    handleCellEnter (row, column, cell, event) {
      if (column) {
        const property = column.property
        if (this.editProp.includes(property)) {
          cell.querySelector('.item_input').style.display = 'block'
        }
      }
    },
    // 列表单元格鼠标离开事件
    handleCellLeave (row, column, cell, event) {
      if (column) {
        const property = column.property
        if (this.editProp.includes(property)) {
          cell.querySelector('.item_input').style.display = 'none'
        }
      }
    },
    // 列表单元格输入框失去焦点事件
    handleBlur(row) {
      editDrawing(row).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    }

  }
};
</script>
